(function(){
	var AjaxCrud = QualityCheck.AjaxCRUD;
	var serverUrl = QualityCheck.GPUD.ServiceConfig;
	var currentPage = 1;
	var pageSize = 10;
	var totalSize;
	$(function(){
		var usercode = $("#user-name",parent.parent.document).attr("code");
		var userkey = $("#user-name",parent.parent.document).attr("key");
		var usertype = $("#user-name",parent.parent.document).attr("usertype");
		keyCheck(usercode,usertype,userkey,function(){
			initSmPages();
			window.onresize = function () {
				initSmPages();
			};
			pagebtns();
			initClass();
		});
	});
	function initClass(){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var userRoles = $("#user-name",parent.parent.document).attr("rolecodes");
		var data = {
				cmd:"getDataRole",
				model:"license",
				userKey:userKey,
				data:{
					rolecode:userRoles
				},
				remark:"",
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			var typeOpt = "";
			var gradeOpt = "";
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var data = res.data;
				for(var i = 0; i < data.length; i++){
					var name = "";
					if(data[i].type == "0"){
						name = "幼儿园";
					}else if(data[i].type == "1"){
						name = "小学";
					}else if(data[i].type == "2"){
						name = "中学";
					}else if(data[i].type == "3"){
						name = "高中";
					}
					typeOpt += "<option value='"+data[i].type+"'>"+name+"</option>";
					
				}
				$("#student-type").html(typeOpt);
				
				var gradeData = data[0].data;
				for(var i = 0; i < gradeData.length; i++){
					gradeOpt += "<option value='"+gradeData[i].grade+"' classcodes='"+gradeData[i].classcode+"' classes='"+gradeData[i].classes+"'>"+gradeData[i].grade+"年级</option>";
				}
				$("#student-grade").html(gradeOpt);
				
				var classes = $("#student-grade option:selected").attr("classes").split(",");
				var classcodes = $("#student-grade option:selected").attr("classcodes").split(",");
				var classOpt = "";
				for(var j = 0; j < classes.length; j++){
					classOpt += "<option value='"+classcodes[j]+"' classes='"+classes[j]+"'>"+classes[j]+"班</option>";
				}
				$("#student-class").html(classOpt);
				
				$("#student-type").unbind("change");
				$("#student-type").change(function(){
					var gradeData = null;
					var type = $("#student-type option:selected").val();
					var gradeOpt = "";
					if(type != ""){
						for(var i = 0; i < data.length; i++){
							if(data[i].type == type){
								gradeData = data[i].data;
							}
						}
						for(var i = 0; i < gradeData.length; i++){
							gradeOpt += "<option value='"+gradeData[i].grade+"' classcodes='"+gradeData[i].classcode+"' classes='"+gradeData[i].classes+"'>"+gradeData[i].grade+"年级</option>";
						}
					}
					$("#student-grade").html(gradeOpt);
					
					var classOpt = "";
					var classes = $("#student-grade option:selected").attr("classes").split(",");
					var classcodes = $("#student-grade option:selected").attr("classcodes").split(",");
					for(var j = 0; j < classes.length; j++){
						classOpt += "<option value='"+classcodes[j]+"' classes='"+classes[j]+"'>"+classes[j]+"班</option>";
					}
					$("#student-class").html(classOpt);
					
					initCourse(1,pageSize);
				});
				
				$("#student-grade").unbind("change");
				$("#student-grade").change(function(){
					var classes = $("#student-grade option:selected").attr("classes").split(",");
					var classcodes = $("#student-grade option:selected").attr("classcodes").split(",");
					var classOpt = "";
					for(var j = 0; j < classes.length; j++){
						if(classcodes[j] == "" && classes[j] == ""){
							classOpt += "<option value='' classes=''>班级</option>";
						}else{
							classOpt += "<option value='"+classcodes[j]+"' classes='"+classes[j]+"'>"+classes[j]+"班</option>";
						}
					}
					$("#student-class").html(classOpt);
					
					initCourse(1,pageSize);
				});
				
				$("#student-class").unbind("change");
				$("#student-class").change(function(){
					initCourse(1,pageSize);
				});
			}else{
				
			}
			initCourse(1,pageSize);
		},null,null);
	}

	var coursePoint;
	function initCourse(){
		processBegin("数据加载中······");
		var userKey = $("#user-name",parent.parent.document).attr("key");	
		var userRoles = $("#user-name",parent.parent.document).attr("rolecodes");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var classcode = $("#student-class option:selected").val();
		var type = $("#student-type option:selected").val();
		var className = "";
		if($("#student-class option:selected").val() != ""){
			className =$("#student-grade option:selected").text() + $("#student-class option:selected").text() + "的";
		}
		$("#course-table-title").text(className + "课程表");
		var data = {
				cmd:"getCourseByStudentCode",
				model:"course",
				userKey:userKey,
				data:{
					rolecode:userRoles,
					classcode:classcode,
					schoolcode:schoolcode,
					type:type
				},
				remark:""
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			var content = "<tr style='font-weight: 700;'>" +
								"<td width='50px'>序号</td>" +
								"<td width='100px'>时间安排</td>" +
								"<td width='100px'>星期一</td>" +
								"<td width='100px'>星期二</td>" +
								"<td width='100px'>星期三</td>" +
								"<td width='100px'>星期四</td>" +
								"<td width='100px'>星期五</td>" +
							"</tr>";
			for(var j = 0; j < 8; j++){
				content += "<tr>" + 
						"<td width='50px'>" + (j+1) + "</td>" +
						"<td style='color:#6F6F6F' class='table-course-date' courseno='" + (j+1) + "' width='121px'>" + getDefaultTime(j+1) + "</td>" +
						"<td style='color:#6F6F6F' class='table-course-point' coursecode='' subjectcodeno='' courseno='" + (j+1) + "' weekno='1' width='100px'>" + "" + "</td>" +
						"<td style='color:#6F6F6F' class='table-course-point' coursecode='' subjectcodeno='' courseno='" + (j+1) + "' weekno='2' width='100px'>" + "" + "</td>" +
						"<td style='color:#6F6F6F' class='table-course-point' coursecode='' subjectcodeno='' courseno='" + (j+1) + "' weekno='3' width='100px'>" + "" + "</td>" +
						"<td style='color:#6F6F6F' class='table-course-point' coursecode='' subjectcodeno='' courseno='" + (j+1) + "' weekno='4' width='100px'>" + "" + "</td>" +
						"<td style='color:#6F6F6F' class='table-course-point' coursecode='' subjectcodeno='' courseno='" + (j+1) + "' weekno='5' width='100px'>" + "" + "</td>" +
					"</tr>";
			}
			$(".course-table-body").empty();
			$(".course-table-body").append(content);
			if(res.result == 1){
				if(res.data != undefined && res.data != ""){
					var data = res.data;
					var list = data.course;
					for(var i = 0; i < list.length; i++){
						for(var key in list[i]){
							if(list[i][key] == null || list[i][key] == "null"){
								list[i][key] = "";
							}
						}
						var courseIndex = parseInt(list[i]["courseno"]*7) + parseInt(list[i]["weekno"]) + 1;
						var timeIndex = parseInt(list[i]["courseno"]*7) + 1;
						var subjectcodeno = list[i]["subjectcodeno"];
						var coursecode = list[i]["coursecode"];
						$(".course-table-body").find("td")[courseIndex].innerHTML = list[i]["subjectcode"];
						$(".course-table-body").find("td")[courseIndex].setAttribute("subjectcodeno",subjectcodeno);
						$(".course-table-body").find("td")[courseIndex].setAttribute("coursecode",coursecode);
						$(".course-table-body").find("td")[timeIndex].innerHTML = list[i]["coursetime"];
					}
				}
				processEnd("加载成功");
			}else{
				processError("加载失败");
			}
			$(".table-course-point").unbind("click");
			$(".table-course-point").click(function(e){
				coursePoint = $(this);
				getAllCourseSubject(function(res){
					var allClass = res;
					if(allClass.length > 0){
						var conts = "<div class='course-plan-dialog-div' subjectcode = ''>清空</div>";
						for(var i = 0; i < allClass.length; i++){
							conts += "<div class='course-plan-dialog-div' subjectcode = '" + allClass[i].subjectcode + "'>" + allClass[i].coursecontent + "</div>";
						}
						var dialogP = $(".index-dialog").position();
						var left = e.pageX - dialogP.left;
						var top = e.pageY - dialogP.top;
						$(".course-dialog").width(302);
						$(".course-plan-dialog").width(300);
						$(".course-plan-dialog").empty();
						$(".course-plan-dialog").append(conts);
						window.setTimeout(function(){
							$("#input-sub-type").show();
							$(".course-dialog").show();}, 70);
						$(".index-dialog-title").unbind("click");
						$(".index-dialog-close1").unbind("click");
						$(".index-dialog-close1").click(function(){
							$(".course-dialog").hide();
						});
						var winW = document.documentElement.clientWidth || document.body.clientWidth;
						var winH = document.documentElement.clientHeight || document.body.clientHeight;
						if((winW - left) < 300){
							left -= 300;
						}
						if(winH - top < $(".course-dialog").height()){
							top -= $(".course-dialog").height();
						}
						$(".course-dialog").css("left",left + "px");
						$(".course-dialog").css("top",top + "px");
						$(".course-plan-dialog div").unbind("click");
						$(".course-plan-dialog div").click(function(){
							if($(this).text() == "清空"){
								coursePoint.text("");
							}else{
								coursePoint.text($(this).text());
							}
							coursePoint.attr("subjectcodeno",$(this).attr("subjectcode"));
							$(".course-dialog").hide();
						});
					}else{
						var conts = "请先到科目管理模块添加课程科目";
						var dialogP = $(".index-dialog").position();
						var left = e.pageX - dialogP.left;
						var top = e.pageY - dialogP.top;
						$(".course-dialog").width(302);
						$(".course-plan-dialog").width(300);
						$(".course-plan-dialog").empty();
						$(".course-plan-dialog").append(conts);
						window.setTimeout(function(){$(".course-dialog").show();}, 70);
						$(".index-dialog-title").unbind("click");
						$(".index-dialog-close1").unbind("click");
						$(".index-dialog-close1").click(function(){
							$(".course-dialog").hide();
						});
						var winW = document.documentElement.clientWidth || document.body.clientWidth;
						var winH = document.documentElement.clientHeight || document.body.clientHeight;
						if((winW - left) < 300){
							left -= 300;
						}
						if(winH - top < $(".course-dialog").height()){
							top -= $(".course-dialog").height();
						}
						$(".course-dialog").css("left",left + "px");
						$(".course-dialog").css("top",top + "px");
					}
				});
			});
			$(".table-course-date").unbind("click");
			$(".table-course-date").click(function(e){
				var me = $(this);
				var thisIndex = me.parent().index();
				var dateArr = getDefaultTime(thisIndex).split("-");
				var conts = "<div class='time-input-se'><input value='"+dateArr[0]+"' type='text' id='start-hour' class='Wdate' readonly='readonly' onclick=\"WdatePicker({dateFmt: 'HH:mm'})\"> - <input type='text' value='"+dateArr[1]+"' id='end-hour' class='Wdate' readonly='readonly' onclick=\"WdatePicker({dateFmt: 'HH:mm'})\"></div><div id='sure-time-btn'>确定</div>";
				var dialogP = $(".index-dialog").position();
				var left = e.pageX - dialogP.left;
				var top = e.pageY - dialogP.top;
				$(".course-dialog").width(242);
				$(".course-plan-dialog").width(240);
				$(".course-plan-dialog").empty();
				$(".course-plan-dialog").append(conts);
				window.setTimeout(function(){
					$("#input-sub-type").hide();
					$(".course-dialog").show();
				}, 70);
				$(".course-dialog").css("left",left + "px");
				$(".course-dialog").css("top",top + "px");
				$("#sure-time-btn").click(function(){
					var time = $("#start-hour").val() + "-" + $("#end-hour").val();
					me.text(time);
					$(".course-dialog").hide();
				});
			});
			
		},null,null);
	}
	function addCourseTable(list){
		processBegin("正在修改中···");
		var userKey = $("#user-name",parent.parent.document).attr("key");	
		var data = {
				cmd:"addOrUpdate",
				model:"course",
				userKey:userKey,
				data:list,
				remark:""
		};
		AjaxCrud.add(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				processEnd("安排课程表成功");
			}else{
				alertBox("unsuccess","安排课程表失败",false);
			}
		},null,null);
	}
	function getAllCourseSubject(callback){
		var list = [];
		var userKey = $("#user-name",parent.parent.document).attr("key");	
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
//		var type = $("input[name='sub-type']:checked").val();
		var type = $("#student-type option:selected").val();
		var data = {
				cmd:"getAllCourseSubject",
				model:"course",
				userKey:userKey,
				data:{
					schoolcode:schoolcode,
					type:type
				},
				remark:"",
				
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			callback(res.data);
		},null,null);
		return list;
	}
	function getDefaultTime(a){
		switch (a){
			case 1:
				return "08:00-08:45";
				break;
			case 2:
				return "08:55-09:40";
				break;
			case 3:
				return "10:00-10:45";
				break;
			case 4:
				return "10:55-11:40";
				break;
			case 5:
				return "13:30-14:15";
				break;
			case 6:
				return "14:25-15:10";
				break;
			case 7:
				return "15:30-16:15";
				break;
			case 8:
				return "16:25-17:10";
				break;
		}
	}
	function leadingInCourses(){
		processBegin("正在导入中···");
		var classcode = $("#leading-class option:selected",parent.parent.parent.document).val();
		var type = $("#leading-type option:selected",parent.parent.parent.document).val();
		var url = Host + "/service/fileupload.do?clazz=exportCourse&classcode="+classcode+"&type="+type;
		var options = {
	            url: url, 
	            type : "POST",
	            success :function(result){
	            	if(result.result == 1){
	            		processEnd("导入成功");
	            	}else{
	            		processError("导入失败");
	            	}
	            },
	    };
	    $("#form",parent.parent.parent.document).ajaxSubmit(options);
	}
	function leadingOutCourses(){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var classcode = $("#student-class option:selected").val();
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var data = {
			cmd: "getCourseByStudentCode",
			model: "course",
			userKey: userKey,
			data:{
				classcode: classcode,
				isdownload: "true",
				schoolcode:schoolcode
//				type:type
			},
			remark:"",						
		};
		processBegin("正在导出中···");
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				if(res.remark.flag == true){
					var url = Host + res.remark.msg;
					window.frames["hrong"].location = url;
					processEnd("导出成功！");
				}else{
					processError("导出失败！");
				}
			}else{
				processError("暂无数据！");
			}
		},null,null);
	}
	function initClass1(){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var userRoles = $("#user-name",parent.parent.document).attr("rolecodes");
		var data = {
				cmd:"getDataRole",
				model:"license",
				userKey:userKey,
				data:{
					rolecode:userRoles
				},
				remark:"",
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			var typeOpt = "<option value='' >学段</option>";
			var gradeOpt = "<option value='' classcodes='' classes=''>年级</option>";
			var classOpt = "<option value='' classes=''>班级</option>";
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var data = res.data;
				for(var i = 0; i < data.length; i++){
					var name = "";
					if(data[i].type == "0"){
						name = "幼儿园";
					}else if(data[i].type == "1"){
						name = "小学";
					}else if(data[i].type == "2"){
						name = "中学";
					}else if(data[i].type == "3"){
						name = "高中";
					}
					typeOpt += "<option value='"+data[i].type+"'>"+name+"</option>";
				}
				$("#leading-type",parent.parent.parent.document).html(typeOpt);
//				initCourse(1,pageSize);
				
				$("#leading-type",parent.parent.parent.document).unbind("change");
				$("#leading-type",parent.parent.parent.document).change(function(){
					var gradeData = null;
					var type = $("#leading-type option:selected",parent.parent.parent.document).val();
					var gradeOpt = "<option value='' classcodes='' classes=''>年级</option>";
					if(type != ""){
						for(var i = 0; i < data.length; i++){
							if(data[i].type == type){
								gradeData = data[i].data;
							}
						}
						for(var i = 0; i < gradeData.length; i++){
							gradeOpt += "<option value='"+gradeData[i].grade+"' classcodes='"+gradeData[i].classcode+"' classes='"+gradeData[i].classes+"'>"+gradeData[i].grade+"年级</option>";
						}
					}
					$("#leading-grade",parent.parent.parent.document).html(gradeOpt);
					
					var classOpt = "<option value='' classes=''>班级</option>";
					$("#leading-class",parent.parent.parent.document).html(classOpt);
				});
				
				$("#leading-grade",parent.parent.parent.document).unbind("change");
				$("#leading-grade",parent.parent.parent.document).change(function(){
					var classes = $("#leading-grade option:selected",parent.parent.parent.document).attr("classes").split(",");
					var classcodes = $("#leading-grade option:selected",parent.parent.parent.document).attr("classcodes").split(",");
					var classOpt = "";
					for(var j = 0; j < classes.length; j++){
						if(classcodes[j] == "" && classes[j] == ""){
							classOpt += "<option value='' classes=''>班级</option>";
						}else{
							classOpt += "<option value='"+classcodes[j]+"' classes='"+classes[j]+"'>"+classes[j]+"班</option>";
						}
					}
					$("#leading-class",parent.parent.parent.document).html(classOpt);
				});
				
			}else{
				alertBox("nochecked",TipsMsg.datapower,false);
			}
		},null,null);
	}
	function pagebtns(){
		$(parent.document).click(function(){
			$(".course-dialog").hide();
		});
		$("input[name='sub-type']").unbind("change");
		$("input[name='sub-type']").change(function(){
			getAllCourseSubject(function(res){
				var allClass = res;
				if(allClass.length > 0){
					var conts = "<div class='course-plan-dialog-div' subjectcode = ''>清空</div>";
					for(var i = 0; i < allClass.length; i++){
						conts += "<div class='course-plan-dialog-div' subjectcode = '" + allClass[i].subjectcode + "'>" + allClass[i].coursecontent + "</div>";
					}
					$(".course-plan-dialog").empty();
					$(".course-plan-dialog").append(conts);
					$(".course-plan-dialog div").unbind("click");
					$(".course-plan-dialog div").click(function(){
						if($(this).text() == "清空"){
							coursePoint.text("");
						}else{
							coursePoint.text($(this).text());
						}
						coursePoint.attr("subjectcodeno",$(this).attr("subjectcode"));
						$(".course-dialog").hide();
					});
				}else{
					var conts = "请先到科目管理模块添加课程科目";
					$(".course-plan-dialog").empty();
					$(".course-plan-dialog").append(conts);
				}
			});
		});
		$("#course-search-btn").click(function(){
			initCourse();
		});
		$("#edit-course-btn").click(function(){
			var coursetds = $(".table-course-point");
			var timetds = $(".table-course-date");
			var classcode = $("#student-class option:selected").val();
			var list = [];
			for(var i = 0; i < coursetds.length; i++){
				var subObj = {};
				for(var j = 0; j < timetds.length; j++){
					if(timetds[j].getAttribute("courseno") == coursetds[i].getAttribute("courseno")){
						subObj.coursetime = timetds[j].innerHTML;
					}
				}
				subObj.courseno = coursetds[i].getAttribute("courseno");
				subObj.weekno = coursetds[i].getAttribute("weekno");
				subObj.subjectcode = coursetds[i].getAttribute("subjectcodeno");
				subObj.coursecode = coursetds[i].getAttribute("coursecode");
				subObj.classcode = classcode;
				if(subObj.coursecode == ""){
					subObj.coursecode = classcode + subObj.subjectcode;
				}
				subObj.semester = 1;
				if(subObj.subjectcode != ""){
					list.push(subObj);
				}
			}
			addCourseTable(JSON.stringify(list));
		});
		$("#edit-courseteacher-btn").click(function(){
			$("#course-form-show").show();
			$("#right-tables").hide();
			$("#course-form-show").attr("src","teachercourse.html");
			$("#student-class").attr("disabled","disabled");
			$("#student-grade").attr("disabled","disabled");
		});
		$("#downloadModel").click(function(){
			deleteBox("确定要下载吗？",function(){
				window.frames["hrong"].location = Host + "/uploads/excle/models/course-model.xls";		
			});
		});
		$("#leadingIn").click(function(){
			leadingDialog(function(){
				var content = "<tr style='height:38px;'>" +
						"<td class='table-left' style='width:60px;'><font color='red'>*</font>班级：</td>" +
						"<td class='table-middle' style='width:230px;'>" +
							"<select id='leading-type' style='width:67px;height:22px;'><option value=''>学段</option></select>" +
							"<select id='leading-grade' style='width:67px;height:22px;margin-left:4px;'><option value=''>年级</option></select>" +
							"<select id='leading-class' style='width:67px;height:22px;margin-left:4px;'><option value=''>班级</option></select>" +
						"</td>" +
					"</tr>" +
					"<tr style='height:38px;'>" +
						"<td class='table-left' style='width:60px;'><font color='red'>*</font>文件：</td>" +
						"<td class='table-middle' style='width:230px;'>" +
							"<input type='file' name='file' id='upload' style='height:21px;width:140px;border:none;'>" +
						"</td>" +
					"</tr>";
				$(".leading-dialog-table",parent.parent.parent.document).html(content);
				initClass1();
				$("#cancelBtn",parent.parent.parent.document).unbind("click");
				$("#cancelBtn",parent.parent.parent.document).click(function(){
					$(".index-dialog-cover",parent.parent.parent.document).hide();
					$(".leading-dialog",parent.parent.parent.document).hide();
				});
				$("#sureBtn",parent.parent.parent.document).unbind("click");
				$("#sureBtn",parent.parent.parent.document).click(function(){
					$(".index-dialog-cover",parent.parent.parent.document).hide();
					$(".leading-dialog",parent.parent.parent.document).hide();
					leadingInCourses();
				});
			});
		});
		$("#leadingOut").click(function(){
			var content = "";
			var studentGrade = $("#student-grade option:selected").text();
			var studentClass = $("#student-class option:selected").text();
			content += studentGrade+"年级"+studentClass+"班";
			deleteBox("您选择的是<span class='span-class'>"+content+"</span>的课程表，</br>确定要导出吗？",function(){
				leadingOutCourses();
			});
		});
	}
	function initSmPages(){
		var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
		$(".right-content").height(rcH);
		
		var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
		$(".content-data-show").height(cdsH);
	}
})();